package com.woniuxy.adminservice.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.admindao.mapper.AdminMapper;
import com.woniuxy.admindao.mapper.AdminRoleMapper;
import com.woniuxy.admindao.model.Admin;
import com.woniuxy.admindao.model.AdminRole;
import com.woniuxy.adminmodel.dto.AdminDto;
import com.woniuxy.adminmodel.param.*;
import com.woniuxy.adminservice.exception.AdminException;
import com.woniuxy.adminservice.service.AdminService;
import com.woniuxy.core.status.AdminStateEnum;
import com.woniuxy.core.util.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 管理员 服务实现类
 * </p>
 *
 * @author 冉杰
 * @since 2024-01-15
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
    @Resource
    private AdminMapper adminMapper;
    @Resource
    private AdminRoleMapper adminRoleMapper;

    /**
     *
     * @param param 新增管理员参数
     */
    @Override
    public void add(AddAdminParam param) {
        //判断手机号是否注册过
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Admin::getAdminPhone, param.getAdminPhone());
        Admin admin = getOne(wrapper);
        if (!BeanUtil.isEmpty(admin)) throw new AdminException(1001, "该手机号已有管理员账号：" + admin.getAdminName());
        //通过id查找出当前在进行操作的管理员账号
        Admin addAdmin = getById(param.getAddAdminId());
        Admin newAdmin = new Admin();
        //取手机后四位生成账户名
        //普通管理员账户名生成
        if (param.getRole() == 2) {
            newAdmin.setAdminName("admin" + param.getAdminPhone().substring(param.getAdminPhone().length() - 4));
        } else {//超级管理员账户名生成
            newAdmin.setAdminName("adminS" + param.getAdminPhone().substring(param.getAdminPhone().length() - 4));
        }
        //取手机后六位生成密码
        newAdmin.setAdminPass(param.getAdminPhone().substring(param.getAdminPhone().length() - 6));
        newAdmin.setAdminPhone(param.getAdminPhone());
        newAdmin.setAdminState(AdminStateEnum.ADMIN_ENABLE.getState());
        newAdmin.setAddAdminAccount(addAdmin.getAdminName());
        newAdmin.setRole(param.getRole());
        newAdmin.setAddTime(DateUtil.formatDateTime(new Date()));
        //往数据库新增管理员
        save(newAdmin);
        //往管理员角色表新增
        AdminRole adminRole = new AdminRole();
        adminRole.setAdminId(newAdmin.getAdminId());
        adminRole.setRoleId(param.getRole());
        adminRoleMapper.insert(adminRole);
    }

    /**
     *
     * @param param 登录参数
     * @return 返回除密码外信息
     */
    @Override
    public AdminDto login(AdminLoginParam param) {
        Admin admin;
        //账户名登录
        if (!BeanUtil.isEmpty(param.getAdminName())) {
            LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Admin::getAdminName, param.getAdminName());
            admin = getOne(wrapper);
        } else {//手机号登录
            LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Admin::getAdminPhone, param.getAdminPhone());
            admin = getOne(wrapper);
        }
        if (BeanUtil.isEmpty(admin)) throw new AdminException(1002, "该管理员账号不存在");
        if (admin.getAdminState().intValue() != AdminStateEnum.ADMIN_ENABLE.getState().intValue()) throw new AdminException(1004, "该管理员账号不可用");
        if (!admin.getAdminPass().equals(param.getAdminPass())) throw new AdminException(1003, "管理员密码输入有误");
        return BeanUtil.toBean(admin,AdminDto.class);
    }

    /**
     *
     * @param param 更改状态，传入更改后的状态枚举
     */
    @Override
    public void changeState(ChangeStateParam param) {
        if (param.getAdminId().intValue()==param.getAdminSId().intValue()) throw new AdminException(1005,"不能操作自己的账号");
        Admin admin = getById(param.getAdminId());
        if (BeanUtil.isEmpty(admin)) throw new AdminException(1002, "该管理员账号不存在");
        admin.setAdminState(param.getAdminState());
        updateById(admin);
    }

    /**
     *
     * @param param 分条件分页查询
     * @return 分页dto
     */
    @Override
    public PageInfo<AdminDto> byPage(AdminByPageParam param) {
        //条件查询
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(!BeanUtil.isEmpty(param.getAdminPhone()),Admin::getAdminPhone,param.getAdminPhone())
                .eq(!BeanUtil.isEmpty(param.getAdminState()),Admin::getAdminState,param.getAdminState())
                .eq(!BeanUtil.isEmpty(param.getRole()),Admin::getRole,param.getRole());
        Page<Admin> page = new Page<>(param.getPageNum(), param.getPageSize());
        adminMapper.selectPage(page,wrapper);
        List<Admin> records = page.getRecords();
        List<AdminDto> adminDtos = BeanUtil.copyToList(records, AdminDto.class);
        PageInfo<AdminDto> pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setPages(page.getPages());
        pageInfo.setList(adminDtos);
        return pageInfo;
    }

    /**
     *
     * @param param 更改的新密码和旧密码
     */
    @Override
    public void changePass(ChangePassParam param) {
        Admin admin = getById(param.getAdminId());
        if (param.getOldPass().equals(param.getNewPass())) throw new AdminException(1006,"旧密码和新密码不能相同");
        if (!admin.getAdminPass().equals(param.getOldPass())) throw new AdminException(1005,"旧密码输入不正确");
        admin.setAdminPass(param.getNewPass());
        updateById(admin);
    }

    /**
     *
     * @param param 账号id
     * @return 除密码外信息
     */
    @Override
    public AdminDto byId(AdminByIdParam param) {
        Admin admin = getById(param.getAdminId());
        if (BeanUtil.isEmpty(admin)) throw new AdminException(1002, "该管理员账号不存在");
        return BeanUtil.toBean(admin, AdminDto.class);
    }
}
